package org.shana.shortlink.core.common.filter;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.shana.shortlink.core.common.convention.result.Results;

import java.io.IOException;
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
//可以使用@Order注解来指定过滤器的执行顺序。在过滤器类上添加@Order注解，并指定一个整数值，值越小的过滤器会先执行。
//@Order(1) 在这里没必要
public class LoginCheckFilter implements Filter {
    public LoginCheckFilter(){
        System.out.println("------------------------------------init filter");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        log.info("访问的请求：{}",requestURI);
//        response.getWriter().write(JSON.toJSONString(Results.failure(null,"NOT LOGIN")));
        filterChain.doFilter(request,response);
        return ;
    };
}

